package com.onec.service.unsafe.lock;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * 显式锁
 */
public class ReenTrantLock {


  static Lock lock = new ReentrantLock();

  // 创建 condition
  static private Condition condition = lock.newCondition();

  // 等待线程的异步任务
  static class WaitTarget implements Runnable {

    @Override
    public void run() {
      lock.lock();
      try {
        // 等待通知释放锁
        try {
          condition.wait();
        } catch (InterruptedException e) {
          e.printStackTrace();
        }

      } finally {

      }
    }
  }

  // 通知线程的异步任务
  static class NOtifyTarget implements Runnable {

    @Override
    public void run() {
      lock.lock();
      try {
        // 发送通知 但是线程没有释放锁
        condition.signal();

      } finally {

      }
    }
  }


  public static void main(String[] args) throws InterruptedException {
    Thread thread = new Thread(new WaitTarget(),"等待锁");
    thread.start();

    TimeUnit.SECONDS.sleep(1);

    Thread notThread = new Thread(new NOtifyTarget(),"通知锁线程");
    notThread.start();


  }
}
